package com.android.dopewars;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;

public class LoanShark extends Activity {

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.loan_shark);
        
        int max_quantity = Math.min(Global.dealer_.debt(), Global.dealer_.cash());
        ((TextView)findViewById(R.id.pay_debt)).setText(Integer.toString(max_quantity));
        
        ((SeekBar)findViewById(R.id.loan_shark_slide)).setMax(max_quantity);
        ((SeekBar)findViewById(R.id.loan_shark_slide)).setProgress(max_quantity);
        ((SeekBar)findViewById(R.id.loan_shark_slide)).setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromTouch) {
				((EditText)findViewById(R.id.pay_debt)).setText(Integer.toString(progress));
			}
			public void onStartTrackingTouch(SeekBar seekBar) {}
			public void onStopTrackingTouch(SeekBar seekBar) {}
        });
        
        ((Button)findViewById(R.id.submit_payment)).setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		// check the validity of the entered text
        		int new_quantity = Integer.parseInt(
        				((EditText)findViewById(R.id.pay_debt)).getText().toString());
        		if (new_quantity < 0) {
        			new_quantity = 0;
        		}
        		int max_payment = Math.min(Global.dealer_.debt(), Global.dealer_.cash());
        		if (new_quantity > max_payment) {
        			new_quantity = max_payment;
        		}
        		Global.dealer_.setDebt(Global.dealer_.debt() - new_quantity);
        		Global.dealer_.setCash(Global.dealer_.cash() - new_quantity);
        		Intent i = new Intent();
        		setResult(RESULT_OK, i);
        		finish();
        	}
        });

		int max_debt = Math.min(Global.maximum_loan_amount_, Global.dealer_.highest_cash_amount());
		int max_loan_quantity = Math.max((int)((6.0/11.0) * (max_debt - Global.dealer_.debt())), 0);
        if (max_quantity == 0) {
        	((TextView)findViewById(R.id.pay_debt)).setText(Integer.toString(max_loan_quantity));
        }
        
        ((SeekBar)findViewById(R.id.loan_shark_get_slide)).setMax(max_loan_quantity);
        ((SeekBar)findViewById(R.id.loan_shark_get_slide)).setProgress(max_loan_quantity);
        ((SeekBar)findViewById(R.id.loan_shark_get_slide)).setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromTouch) {
				((EditText)findViewById(R.id.pay_debt)).setText(Integer.toString(progress));
			}
			public void onStartTrackingTouch(SeekBar seekBar) {}
			public void onStopTrackingTouch(SeekBar seekBar) {}
        });
        
        ((Button)findViewById(R.id.get_another_loan)).setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		int max_debt = Math.min(Global.maximum_loan_amount_, Global.dealer_.highest_cash_amount());
        		int max_loan_quantity = Math.max((int)((6.0/11.0) * (max_debt - Global.dealer_.debt())), 0);
        		int new_quantity = Integer.parseInt(
        				((EditText)findViewById(R.id.pay_debt)).getText().toString());
        		if (new_quantity < 0) {
        			new_quantity = 0;
        		}
        		if (new_quantity > max_loan_quantity) {
        			new_quantity = max_loan_quantity;
        		}
        		Global.dealer_.setDebt(Global.dealer_.debt() + (int)((11.0/6.0) * new_quantity));
        		Global.dealer_.setCash(Global.dealer_.cash() + new_quantity);
        		Intent i = new Intent();
        		setResult(RESULT_OK, i);
        		finish();
        	}
        });
        
        WindowManager.LayoutParams dialog_params = getWindow().getAttributes();
    	dialog_params.width = WindowManager.LayoutParams.FILL_PARENT;
    	getWindow().setAttributes(dialog_params);
    }
    
    int max_payment_;
}
